package com.aramis.mydytt.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.aramis.mydytt.R
import com.aramis.mydytt.ext.dip
import com.aramis.mydytt.ext.logE
import com.aramis.mydytt.ext.screenHeight

/**
 * Description: BottomDialog
 * @author lizhidan
 * Created: 2021/5/8
 */
abstract class BottomDialog(protected  val activity: Activity) : Dialog(activity, R.style.BottomDialog) {

    init {
        init()
    }

    protected var mDecorView: View? = null
    private fun init() {
        window?.apply {
            mDecorView = decorView
            val windowParams = this.attributes
            windowParams.width = WindowManager.LayoutParams.MATCH_PARENT
            windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            windowParams.horizontalMargin = 0f
            setWindowAnimations(R.style.BottomDialog_Animation)
            setGravity(Gravity.BOTTOM)
            mDecorView?.setPadding(0, 0, 0, 0)
        }
        val view = layoutInflater.inflate(getLayoutId(), null)
        setContentView(view)
        onInit()
    }

    protected open fun onInit() {
    }

    abstract fun getLayoutId(): Int

    protected fun underCoverHeight(view: View?) {
        if (view != null) {
            val coverImageHeight = context.resources.getDimensionPixelSize(R.dimen.height_detail_cover)
            val viewHeight = context.screenHeight() - coverImageHeight + dip(47)
            if (view.layoutParams != null) {
                view.layoutParams.height = viewHeight
            } else {
                view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, viewHeight)
            }
        } else {
            logE("underCoverHeight view is null")
        }
    }

    protected fun <T : View> findView(id: Int): T? {
        return mDecorView?.findViewById<T>(id)
    }
}